home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / lt_layoutmenus.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  6KB  |  239 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <stdarg.h>
  17.  
  18. /*****************************************************************************/
  19.  
  20. #include "Assert.h"
  21.  
  22. /*****************************************************************************/
  23.  
  24. struct Menu *
  25. LT_LayoutMenus(LayoutHandle *handle,struct NewMenu *menuTemplate,...)
  26. {
  27.     struct Menu    *Result;
  28.     va_list      VarArgs;
  29.  
  30.     va_start(VarArgs,menuTemplate);
  31.     Result = LT_LayoutMenusA(handle,menuTemplate,(struct TagItem *)VarArgs);
  32.     va_end(VarArgs);
  33.  
  34.     return(Result);
  35. }
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41. /****** gtlayout.library/LT_LayoutMenusA ******************************************
  42. *
  43. *   NAME
  44. *    LT_LayoutMenusA -- Create a menu according to a template.
  45. *
  46. *   SYNOPSIS
  47. *    Menu = LT_LayoutMenusA(Handle,Template,Tags);
  48. *     D0                      A0     A1      A2
  49. *
  50. *    struct Menu *LT_LayoutMenusA(LayoutHandle *,struct NewMenu *,
  51. *
  52. *        struct TagItem *);
  53. *
  54. *    Menu = LT_LayoutMenus(Handle,Template,...);
  55. *
  56. *    struct Menu *LT_LayoutMenus(LayoutHandle *,struct NewMenu *,...);
  57. *
  58. *   FUNCTION
  59. *    Unlike the corresponding routines in gadtools.library
  60. *    LT_LayoutMenusA() will both create and layout a menu.
  61. *    Also included is locale support.
  62. *
  63. *   INPUTS
  64. *    Handle - Pointer to LayoutHandle structure.
  65. *
  66. *    Template - Address of a ready-to-use NewMenu table to
  67. *        create the menu from.
  68. *
  69. *    Tags - Tagitem list to control menu attributes
  70. *
  71. *    Tags:
  72. *
  73. *    LAMN_FirstLabel (LONG) - Locale ID of the first string to
  74. *        use as a menu title/item/subitem label. This tag
  75. *        works in conjunction with LA_LastLabel.
  76. *
  77. *    LAMN_LastLabel (LONG) - Locale ID of the last string to
  78. *        use as a menu title/item/subitem label. This tag
  79. *        works in conjunction with LA_FirstLabel. The code
  80. *        will loop through FirstLabel..LastLabel and assign
  81. *        the corresponding locale text for each ID to the
  82. *        NewMenu.nm_Label entries. Labels which are already
  83. *        initialized with NM_BARLABEL are skipped.
  84. *
  85. *    LAMN_LabelTable (LONG *) - Pointer to an array of IDs
  86. *        to use for building the menu labels. This requires
  87. *        that a locale hook is provided with the layout handle.
  88. *        The array is terminated by -1.
  89. *
  90. *   RESULT
  91. *    Menu - Pointer to a Menu structure. You can free this
  92. *           using gadtools.library/FreeMenus().
  93. *
  94. ******************************************************************************
  95. *
  96. */
  97.  
  98. struct Menu * LIBENT
  99. LT_LayoutMenusA(REG(a0) LayoutHandle *handle,REG(a1) struct NewMenu *menuTemplate,REG(a2) struct TagItem *TagParams)
  100. {
  101.     struct Menu *menu;
  102.  
  103.     menu = NULL;
  104.  
  105.     if(handle)
  106.     {
  107.         struct TagItem    *tag,*List = TagParams;
  108.         LONG             label = 0,last = 0;    /* For the sake of the compiler, initialize these. */
  109.         LONG            *Table = NULL;
  110.         ULONG             allocSize = 0;    /* For the sake of the compiler, initialize this. */
  111.         BOOL              foundFirst;
  112.         BOOL              foundLast;
  113.  
  114.         foundFirst    = FALSE;
  115.         foundLast    = FALSE;
  116.  
  117.         while(tag = NextTagItem(&List))
  118.         {
  119.             switch(tag->ti_Tag)
  120.             {
  121.                 case LA_LabelTable:
  122.  
  123.                     Table = (LONG *)tag->ti_Data;
  124.                     break;
  125.  
  126.                 case LA_FirstLabel:
  127.  
  128.                     foundFirst = TRUE;
  129.  
  130.                     label = tag->ti_Data;
  131.                     break;
  132.  
  133.                 case LA_LastLabel:
  134.  
  135.                     foundLast = TRUE;
  136.  
  137.                     last = tag->ti_Data;
  138.                     break;
  139.             }
  140.         }
  141.  
  142.         if(foundFirst == foundLast || Table)
  143.         {
  144.             struct NewMenu *localTemplate = NULL;
  145.  
  146.             if(Table)
  147.             {
  148.                 foundFirst = FALSE;
  149.  
  150.                 if(handle->LocaleHook)
  151.                 {
  152.                     LONG count;
  153.  
  154.                     count = 0;
  155.  
  156.                     while(menuTemplate[count].nm_Type != NM_END)
  157.                         count++;
  158.  
  159.                     count++;
  160.  
  161.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  162.                     {
  163.                         LONG i;
  164.  
  165.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  166.  
  167.                         for(i = 0 ; *Table != -1 && i < count ; i++)
  168.                         {
  169.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  170.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)(*Table++));
  171.                         }
  172.                     }
  173.                 }
  174.  
  175.                 menuTemplate = localTemplate;
  176.             }
  177.  
  178.             if(foundFirst)
  179.             {
  180.                 if(handle->LocaleHook)
  181.                 {
  182.                     LONG count;
  183.  
  184.                     count = 0;
  185.  
  186.                     while(menuTemplate[count].nm_Type != NM_END)
  187.                         count++;
  188.  
  189.                     count++;
  190.  
  191.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  192.                     {
  193.                         LONG i;
  194.  
  195.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  196.  
  197.                         if(foundLast)
  198.                             count = last - label + 1;
  199.  
  200.                         for(i = 0 ; i < count ; i++)
  201.                         {
  202.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  203.                             {
  204.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)label);
  205.  
  206.                                 label++;
  207.                             }
  208.                         }
  209.                     }
  210.                 }
  211.  
  212.                 menuTemplate = localTemplate;
  213.             }
  214.  
  215.             if(menuTemplate)
  216.             {
  217.                 if(menu = CreateMenusA(menuTemplate,NULL))
  218.                 {
  219.                     if(!LayoutMenus(menu,handle->VisualInfo,
  220.                         GTMN_NewLookMenus,    TRUE,
  221.  
  222.                         handle->AmigaGlyph ? GTMN_AmigaKey : TAG_IGNORE,handle->AmigaGlyph,
  223.                         handle->CheckGlyph ? GTMN_Checkmark : TAG_IGNORE,handle->CheckGlyph,
  224.                     TAG_DONE))
  225.                     {
  226.                         FreeMenus(menu);
  227.  
  228.                         menu = NULL;
  229.                     }
  230.                 }
  231.             }
  232.  
  233.             LTP_Free(handle,localTemplate,allocSize);
  234.         }
  235.     }
  236.  
  237.     return(menu);
  238. }
  239.